import json


class JsonResponse(object):

    def __init__(self):
        self._code = 10000
        self._msg = 'success'
        self._data = {}

    def to_str(self) -> str:
        print(json.dumps({'code': self._code, 'msg': self._msg, 'data': self._data}), flush=True)
        return json.dumps({'code': self._code, 'msg': self._msg, 'data': self._data})

    def set_code(self, code: int):
        self._code = code

    def set_msg(self, msg: str):
        self._msg = msg

    def set_data(self, data):
        self._data = data

    def set_data_item(self, key, value):
        self._data[key] = value


class ErrorResponse(JsonResponse):

    def __init__(self, msg: str):
        super().__init__()
        self._code = 10301
        self._msg = msg


class FuncErrorResponse(JsonResponse):

    def __init__(self, msg: str):
        super().__init__()
        self._code = 10302
        self._msg = msg
